﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.ApplicationPages;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint;
using Winwise.SPWebConfig.Core;

namespace Winwise.SPWebConfig.UI
{
    public abstract class WebConfigDialogAdminPageBase : DialogAdminPageBase
    {
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (!Page.IsPostBack)
            {
                if (Request.QueryString["New"] != null)
                {
                    UpdateMessage("Web Config modification successfully created");
                }
            }
        }

        public void ApplyWebConfigModif(SPWebApplication webapp)
        {
            if (webapp != null)
            {
                if (Request.QueryString["ModifId"] != null)
                {
                    Guid modifId = new Guid(Request.QueryString["ModifId"]);

                    using (SPLongOperation longOperation = new SPLongOperation(this))
                    {
                        longOperation.LeadingHTML = "Applying modification on all servers...";
                        longOperation.TrailingHTML = "Wait until the end of the operation...";

                        longOperation.Begin();

                        webapp.ApplyCustomWebConfigModifications(modifId);
                        webapp.WaitUntilEndOfWebConfigModification();

                        longOperation.End(string.Format("/_admin/Winwise.SPWebConfig/ManageWebConfigModif.aspx?WebApplicationId={0}", webapp.Id.ToString("N")));
                    }
                }
            }
        }

        public abstract void FillWebConfigModif(WebConfigModification modif);
        public abstract void DataBind(WebConfigModification modif);
        public abstract void UpdateMessage(string message);
        public abstract void UpdateErrorMessage(string message);
        public virtual void DataBindEmpty() { }


        public void AddEditModif<T>(SPWebApplication webapp) where T : WebConfigModification,new()
        {
            WebConfigFileChanges changes = webapp.GetCustomWebConfigModifications();
            try
            {
                if (Request.QueryString["ModifId"] != null)
                {
                    Guid modifId = new Guid(Request.QueryString["ModifId"]);
                    var modif = changes.WebModif.Find(m => m.ID == modifId);
                    FillWebConfigModif(modif);
                    changes.Update();
                    UpdateMessage("XSL Modification successfully modified.");
                }
                else
                {
                    T modif = new T();
                    FillWebConfigModif(modif);
                    changes.WebModif.Add(modif);
                    changes.Update();
                    Response.Redirect(string.Format("{0}&ModifId={1}&New=true", Request.Url.OriginalString, modif.ID.ToString()));
                }
            }
            catch (Exception e)
            {
                UpdateErrorMessage("Cannot save web.config modification : " + e.ToString());
            }

        }

        public string GetManageLink(SPWebApplication webapp)
        {
            return string.Format("/_admin/Winwise.SPWebConfig/ManageWebConfigModif.aspx?WebApplicationId={0}", webapp.Id.ToString("N")); ;
        }

        public void OnContextChange(SPWebApplication webapp)
        {
            if (webapp != null)
            {

                WebConfigFileChanges changes = webapp.GetCustomWebConfigModifications();
                if (Request.QueryString["ModifId"] != null)
                {
                    Guid modifId = new Guid(Request.QueryString["ModifId"]);
                    var modif = changes.WebModif.Find(m => m.ID == modifId);
                    DataBind(modif);
                }
                else
                {
                    DataBindEmpty();
                }
            }
        }
    }
}
